package com.CST.controller;

import com.CST.common.api.Response;
import com.CST.common.api.ResultObject;
import com.CST.dto.BaseCostDTO;
import com.CST.dto.DateDTO;
import com.CST.entity.BaseCost;
import com.CST.entity.CostDataInfo;
import com.CST.mapper.cst.BaseSalaryMapper;
import com.CST.service.IAllocationCoefficientService;
import com.CST.service.IBaseCostService;
import com.CST.service.IBaseSalaryService;
import com.CST.service.ThreadService;
import com.CST.util.PageHelperTool;
import com.CST.util.ThreadPoolProxy;
import com.CST.vo.BaseCostNewVO;
import com.CST.vo.ReportBusinessVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/baseCost")
@Api(tags = "成本数据")
public class BaseCostController {

    @Autowired
    private IBaseCostService baseCostService;

    @Autowired
    private BaseSalaryMapper baseSalaryMapper;

    @Autowired
    private IBaseSalaryService baseSalaryService;

    @Autowired
    private ThreadPoolProxy threadPoolProxy;

    @Autowired
    private IAllocationCoefficientService allocationCoefficientService;

    @Autowired
    private ThreadService  threadService;
    @PostMapping("/getAllBaseCostByDate")
    @ApiOperation("根据时间和院区信息获取各会计分类成本总额")
    public ResultObject getAllBaseCostByDate(@RequestBody DateDTO dateDTO) {
        return baseCostService.getAllBaseCostByDate(dateDTO);
    }

    @PostMapping("/listBaseCostGroupDept")
    @ApiOperation("根据年月计算各个科室的会计分类总额")
    public ResultObject listBaseCostGroupDept(@RequestBody DateDTO dateDTO) {
        return baseCostService.listBaseCostGroupDept(dateDTO);
    }
    @PostMapping("/listBaseCostDeptByDate")
    @ApiOperation("核算科室成本汇总表")
    public ResultObject listBaseCostDeptByDate(@RequestBody DateDTO dateDTO) {
        return baseCostService.listBaseCostDeptByDate(dateDTO);
    }

    @PostMapping("/download10")
    public void exportExcel10(@RequestBody DateDTO dateDTO, HttpServletResponse response) throws IOException {
        baseCostService.exportExcel10(response,dateDTO);
    }
    @PostMapping("/download")
    public void exportExcel(@RequestBody DateDTO dateDTO, HttpServletResponse response){
        baseCostService.exportExcel(response,dateDTO);
    }

    @PostMapping("/download/{code}")
    public void exportExcel(@RequestBody DateDTO dateDTO, HttpServletResponse response,@PathVariable String code) throws IOException {
        baseCostService.exportExcelDepartment(response,dateDTO,code);
    }

    @PostMapping("/download5")
    public void exportExcel5(@RequestBody DateDTO dateDTO, HttpServletResponse response) throws IOException {
        baseCostService.exportExcel5(response,dateDTO);
    }

    @PostMapping("/download7")
    public void exportExcel7(@RequestBody DateDTO dateDTO, HttpServletResponse response) throws IOException {
        baseCostService.exportExcel7(response,dateDTO);
    }

    @PostMapping("/download8")
    public void exportExcel8(@RequestBody DateDTO dateDTO, HttpServletResponse response) throws IOException {
        baseCostService.exportExcel8(response,dateDTO);
    }

    @PostMapping("/listBaseCostByPDeptCode/{pDeptCode}")
    @ApiOperation("根据日期+院区+父级科室编码获取子类成本明细")
    public ResultObject listBaseCostByPDeptCode(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode) {
        return baseCostService.listBaseCostByPDeptCode(dateDTO, pDeptCode);
    }

    /**
     * 根据传入的父级科室id查询对应的会计分类明细
     * 条件：年月+父级科室id+院区+会计分类编码
     * 适用：药品费，卫生材料费，其他费用
     */
    @PostMapping("/listCostInfoByPDeptCodeAndUnitCode/{pDeptCode}")
    @ApiOperation("根据传入的父级科室id查询对应的明细")
    public ResultObject listCostByPDeptCodeAndUnitCode(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode) {
        return baseCostService.listCostInfoByPDeptCodeAndUnitCode(dateDTO, pDeptCode);
    }

    /**
     * 获取物品二级明细
     *
     * @param dateDTO
     * @param pDeptCode
     * @return
     */
    @PostMapping("/listCostByPDeptCodeAndUnitCode/{pDeptCode}")
    @ApiOperation("根据传入的父级科室id查询对应的明细的明细")
    public ResultObject listCostInfoByPDeptCodeAndUnitCode(@RequestBody DateDTO dateDTO, @PathVariable String pDeptCode) {
        return baseCostService.listCostByPDeptCodeAndUnitCode(dateDTO, pDeptCode);
    }


    @PostMapping("/listMaterialsCategorySummary/{deptCode}")
    @ApiOperation("根据传入的科室id查询对应的明细")
    public ResultObject listMaterialsCategorySummary(@RequestBody DateDTO dateDTO, @PathVariable String deptCode) {
        return baseCostService.listMaterialsCategorySummary(dateDTO, deptCode);
    }

    /**
     * 根据传入的科室id查询对应的明细
     * 条件：年月+科室id+院区+会计分类编号
     */
    @PostMapping("/listCostByDeptCodeAndUnitCode/{deptCode}")
    @ApiOperation("根据传入的科室id查询对应的明细")
    public ResultObject listCostByDeptCodeAndUnitCode(@RequestBody DateDTO dateDTO, @PathVariable String deptCode) {
        return baseCostService.listCostByDeptCodeAndUnitCode(dateDTO, deptCode);
    }

    @PostMapping("/listBaseCostWithCostItem")
    @ApiOperation("根据传入的年月范围+院区(按成本项目汇总)")
    public ResultObject listBaseCostWithCostItem(@RequestBody DateDTO dateDTO) {
        if (!dateDTO.getStarYear().equals(dateDTO.getEndYear())) {
            return ResultObject.successReturn("该页面不支持跨年查询");
        }
        return baseCostService.listBaseCostWithCostItem(dateDTO);
    }

    @PostMapping("/listCostWithPDept")
    @ApiOperation("核算科室收入成本汇总表(核算定义)")
    public ResultObject listCostWithPDept(@RequestBody DateDTO dateDTO) {
        if (!dateDTO.getStarYear().equals(dateDTO.getEndYear())) {
            return ResultObject.successReturn("该页面不支持跨年查询");
        }
        return baseCostService.listCostWithPDept(dateDTO);
    }

    @PostMapping("/listCostWithDept")
    @ApiOperation("核算科室收入成本汇总表(核算单元)")
    public ResultObject listCostWithDept(@RequestBody DateDTO dateDTO) {
        if (!dateDTO.getStarYear().equals(dateDTO.getEndYear())) {
            return ResultObject.successReturn("该页面不支持跨年查询");
        }
        return baseCostService.listCostWithDept(dateDTO);
    }

    @PostMapping("/insertBaseCost")
    @ApiOperation("采集成本数据")
    public Response insertBaseCost(@RequestBody CostDataInfo costDataInfo) throws InterruptedException {
        threadService.insertCostData(costDataInfo);
        Thread.sleep(15000);
        return Response.success("采集成本数据成功");

    }

    @PostMapping("/listBaseCost")
    @ApiOperation("获取成本数据")
    public ResultObject listBaseCost(@RequestBody CostDataInfo costDataInfo) {
        try {
            List<BaseCost> costDataInfoList = baseCostService.listBaseCost(costDataInfo);
            // 计算总额
            BigDecimal amount = BigDecimal.ZERO;
            for (BaseCost baseCost : costDataInfoList) {
                amount = amount.add(baseCost.getAmount());
            }
            PageInfo<BaseCost> baseCostPageInfo = PageHelperTool.excutePageInfo(costDataInfoList, costDataInfo.getPageIndex(), costDataInfo.getPageSize());
            List<BaseCost> list = baseCostPageInfo.getList();
            List<BaseCostNewVO> vos = new ArrayList<>();
            list.forEach(e->{
                BaseCostNewVO vo = new BaseCostNewVO();
                BeanUtils.copyProperties(e,vo);
                vo.setTime(LocalDate.of(e.getYear(),e.getMonth(),1));
                vos.add(vo);
            });
            PageInfo<BaseCostNewVO> info = new PageInfo<>();
            info.setList(vos);
            info.setTotal(baseCostPageInfo.getTotal());
            Map map = new HashMap<>();
            map.put("data", info);
            map.put("amount", amount);
            return ResultObject.successReturn(map);
        } catch (Exception e) {
            return ResultObject.errorReturn("获取成本数据出错");
        }
    }

    @PostMapping("/addBaseCost")
    @ApiOperation("添加成本数据")
    public ResultObject addBaseCost(@RequestBody BaseCostDTO dto) {
        return ResultObject.success(baseCostService.saveOrUpdateBaseCost(dto));
    }

    @GetMapping("/remove/{id}/{year}")
    @ApiOperation("删除成本数据")
    public ResultObject remove(@PathVariable long id, @PathVariable int year) {
        return ResultObject.success(baseCostService.removeBaseCost(id, year));
    }

    @PostMapping("/listCostAndInComeByPDept/{pDeptCode}")
    @ApiOperation("科室经营分析报表")
    public ResultObject listCostAndInComeByPDept(@PathVariable String pDeptCode, @RequestBody DateDTO dateDTO) {
        List<ReportBusinessVO> list = baseCostService.listCostAndInComeByPDept(dateDTO, pDeptCode);
        return ResultObject.successReturn(list);
    }

    @PostMapping("/getDeptBaseCost")
    @ApiOperation("季度科室成本表")
    public ResultObject getDeptBaseCost(@RequestBody DateDTO dateDTO) {
        return ResultObject.successReturn( baseCostService.getDeptBaseCost(dateDTO));
    }

    @PostMapping("/download11")
    public void exportExcel11(@RequestBody DateDTO dateDTO, HttpServletResponse response){
        baseCostService.exportExcel11(response,dateDTO);
    }

}
